<template>
	<view class="userInfo-content">
		<view class="info">
			<!-- 头像 -->
			<view class="avatar" style="margin:20rpx 0;">
				<u-avatar :src="arrayGet(data.userInfo,'image.path_url','')" size="50"></u-avatar>
				<view style="margin-left: 40rpx;" @click="uploadAvatarImage">
					<u--text text="更改头像" type="primary"></u--text>
				</view>
			</view>
			<view>
				<u-cell-group>
					<u-cell size="large" title="昵称" :value="data.userInfo?.show_name"></u-cell>
					<u-cell size="large" title="手机号" :value="data.userInfo?.member_cellphone_secret"></u-cell>
					<u-cell size="large" title="性别"
						:value="data.userInfo?.sex_str?data.userInfo?.sex_str:'保密'"></u-cell>
					<u-cell size="large" title="年龄" :value="data.userInfo?.age?data.userInfo?.age:'保密'"></u-cell>
					<u-cell size="large" title="推荐码"
						:value="data.userInfo?.promotion_code?data.userInfo.promotion_code:'添加推荐码'"
						@click="addPromotionCode" :isLink="data.userInfo.promotion_code?false:true"></u-cell>
					<u-cell size="large" title="设置登录密码" isLink url="/pages/my/person/userInfo/password"></u-cell>
					<u-cell size="large" title="设置签署密码" url="/pages/my/person/userInfo/signPassword" isLink></u-cell>
					<u-cell size="large" :title="userType == 1?'去企业':'去个人'" isLink @click="changeUserType"></u-cell>
				</u-cell-group>
			</view>
		</view>
		<view class="flex button-box">
			<u-button type="primary" text="修改昵称" class="custom-style" @click="showModal"
				style="margin-right: 40rpx;"></u-button>
			<view style="width: 40rpx;"></view>
			<u-button type="error" :plain="true" text="退出登录" class="custom-style" @click="loginOut"></u-button>
		</view>
	</view>
	<!-- 修改昵称 -->
	<u-modal :show="show" title="修改昵称" :show-cancel-button="true" @confirm="confirm" @cancel="show=false">
		<view class="slot-content">
			<u--input placeholder="请输入昵称" border="surround" v-model="nickName"></u--input>
		</view>
	</u-modal>
	<u-modal :show="show_" title="添加推荐码" :show-cancel-button="true" @confirm="confirm_" @cancel="show_=false">
		<view class="slot-content">
			<u--input placeholder="请输入推荐人推荐码" border="surround" v-model="promotion_code"></u--input>
		</view>
	</u-modal>
</template>
<script setup>
	import {
		memberInfo,
		memberSwitchLogin,
		editUserInfo
	} from "../../../../api/module/mine.js";
	import {
		memberLoginOut
	} from "../../../../api/module/login.js";
	import {
		uploadImageBase64
	} from "../../../../api/module/common.js";
	import {
		ref,
		reactive,
		onBeforeMount
	} from "vue"
	import {
		useStore
	} from 'vuex';
	import {
		arrayGet,
		goRedirect,
		goSwitchTab
	} from "../../../../utils/common";
	const store = useStore()
	const userType = store.state.userType;
	const data = reactive({
		userInfo: {},
		fileList6: [],
	})
	const show = ref(false)
	const show_ = ref(false)
	const nickName = ref('')
	const promotion_code = ref('')
	onBeforeMount(() => {
		getUserInfo()
	})
	const loginOut = () => {
		memberLoginOut().then(res => {
			uni.setStorageSync("token", '')
			uni.setStorageSync("userInfo", null)
			getCurrentPages().length - 1;
			const pages = getCurrentPages()
			// 获取加载的页面
			goRedirect('/pages/alogin/index');
		})
	}
	const showModal = () => {
		nickName.value = ''
		show.value = true
	}
	const confirm = () => {
		if (!nickName.value) {
			uni.$u.toast('请输入昵称')
			return
		}
		let params = {
			id: data.userInfo.id,
			nick_name: nickName.value
		}
		editUserInfo(params).then(resule => {
			getUserInfo()
			show.value = false
		})
	}
	const confirm_ = () => {
		if (!promotion_code.value) {
			uni.$u.toast('请输入推荐码')
			return
		}
		let params = {
			id: data.userInfo.id,
			promotion_code: promotion_code.value
		}
		editUserInfo(params).then(resule => {
			getUserInfo()
			show_.value = false
		})
	}
	const changeUserType = () => {
		memberSwitchLogin().then(res => {
			uni.setStorageSync("token", res.token)
			uni.setStorageSync("userInfo", res.user)
			store.commit('changeUserType', 2)
			goSwitchTab('/pages/my/index');
		})
	}
	const getUserInfo = () => {
		memberInfo().then(res => {
			data.userInfo = res
		}).catch(err => {
			console.log(err)
		})
	}
	const uploadAvatarImage = () => { //选择图片  转base64
		uni.chooseImage({
			count: 1, //上传图片数量
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有
			success: (chooseImageRes) => {
				//tempFilePaths 图片的本地文件路径列表
				const tempFilePaths = chooseImageRes.tempFilePaths[0];
				uni.getFileSystemManager().readFile({
					filePath: tempFilePaths, // 要读取的文件路径
					encoding: 'base64', // 编码格式
					success: function(res) {
						uploadAndEditImage(res.data)
					}
				})
			}
		});
	}
	const uploadAndEditImage = (url) => {
		let imageParams = {
			ext: 'png',
			content: url,
			org_name: new Date().getTime() + '.png'
		}
		uploadImageBase64(imageParams).then(res => {
			let image = []
			image.push(res.id)
			let params = {
				id: data.userInfo.id,
				image
			}
			editUserInfo(params).then(resule => {
				getUserInfo()
			})
		})
	}
	const addPromotionCode = () => {
		if (data.userInfo.promotion_code) {
			return;
		} else {
			promotion_code.value = '';
			show_.value = true
		}
	}
</script>
<style lang="less" scoped src="/static/css/my/userInfo.scss"></style>